package com.ruoyi.elase.service;


import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.elase.entity.Chat;
import com.ruoyi.elase.entity.ResponseEntity;
import com.ruoyi.elase.mapper.CharMapper;
import net.sf.json.JSONObject;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CharServiceimpl {

    @Resource
    private CharMapper charMapper;

    @Resource
    private RestHighLevelClient restHighLevelClient;

    public ResponseEntity All() {

        IndexRequest indexRequest = new IndexRequest("han", "_doc");
        //查询数据库的数据
        List<Chat> list=charMapper.getList();
        //循环获取
        for (Chat chat : list) {
            indexRequest.id(chat.getId().toString());
            Map map=new HashMap();
            map.put("chatName",chat.getChatName());
            indexRequest.source(map);
            try {
                restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return new ResponseEntity("同步成功");
    }




    public ResponseEntity seach(String messageValue) throws IOException {
        SearchRequest searchRequest=new SearchRequest("han");
        searchRequest.types("_doc");
        //放到最外  方便使用
        String newchatName = "";
        //拆分成单个  从字符串变成char类型[]
        char[] chars = messageValue.toCharArray();
        //拆分成单个 循环比较前端传过来的数据尽心比较
        for (char c : chars) {
            //再次转成字符串
            String chatName = String.valueOf(c);
            //查询条件
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            //查询
            if (StringUtils.isNotBlank(messageValue)){
                MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery(chatName, "chatName");
                boolQuery.must(multiMatchQuery);
            }
            searchSourceBuilder.query(boolQuery);
            searchRequest.source(searchSourceBuilder);
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

            String newName = "";

            SearchHit[] hits = search.getHits().getHits();
            //从集合中循环获取前端传过来的数据
            for (SearchHit hit : hits) {
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                newName = (String) sourceAsMap.get("chatName");
            }
            if (StringUtils.isNotBlank(newName)){
                newchatName += "*";
            }else{
                newchatName += chatName;
            }
        }
        return new ResponseEntity(newchatName);
    }
}
